define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {


                    del_url: 'aivideo/del',


                }
            });
//绑定事件
            $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                var panel = $($(this).attr("href"));
                if (panel.size() > 0) {
                    Controller.table[panel.attr("id")].call(this);
                    $(this).on('click', function (e) {
                        $($(this).attr("href")).find(".btn-refresh").trigger("click");
                    });
                }
                //移除绑定的事件
                $(this).unbind('shown.bs.tab');
            });

            //必须默认触发shown.bs.tab事件
            $('ul.nav-tabs li.active a[data-toggle="tab"]').trigger("shown.bs.tab");
        },
        table: {
            first: function () {
                // 表格1
                var table1 = $("#table1");
                table1.bootstrapTable({
                    url: 'aivideo/index',
                    pk: 'id',
                    toolbar: '#toolbar1',
                    sortName: 'id',
                    search: false,
                    columns: [
                        [
                            {field: 'id', title: 'ID'},
                            {field: 'local', title: __('Url'), formatter: function (value, row, index) {
                                    value = value === null ? '' : value.toString();
                                    var classname = typeof this.classname !== 'undefined' ? this.classname : 'img-lg img-center';
                                    var html = [];
                                    if(row['type']==1){
                                        var arr = value.split(',');
                                        $.each(arr, function (i, value) {
                                            value = value ? value : '/assets/img/blank.gif';
                                            html.push('<a href="javascript:"><img style="width:300px;height: 300px " src="' + Fast.api.cdnurl(value) + '" /></a>');
                                        });
                                        return html.join(' ');
                                    }
                                    else {

                                        html.push('<video style="width:300px;height: 300px " src="' + Fast.api.cdnurl(row['local']) + '"  controls="controls" />');
                                        return html;
                                    }

                                }},
                            {field: 'activity.title', title: __('活动名称')},
                            {field: 'title', title: __('视频标题')},
                            {field: 'exception', title: __('错误原因')},
                            {field: 'createtime', title: __('生成时间'),formatter: Table.api.formatter.datetime},
                            {field: 'operate', title: __('Operate'), table: table1, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                        ]
                    ]
                });

                // 为表格1绑定事件
                Table.api.bindevent(table1);
                Controller.api.bindevent();
            },


            third: function () {
                // 表格1
                var table2 = $("#table2");
                table2.bootstrapTable({
                    url: 'aivideo/index2',
                    pk: 'id',
                    toolbar: '#toolbar2',
                    sortName: 'id',
                    search: false,
                    columns: [
                        [
                            {field: 'id', title: 'ID'},
                            {field: 'activity.title', title: __('活动名称')},
                            {field: 'name', title: __('视频标题')},
                            {field: 'video_nums', title: __('合成视频数量')},
                            {field: 'note', title: __('合成来源')},
                            {field: 'createtime', title: __('生成时间'),formatter: Table.api.formatter.datetime},
                        ]
                    ]
                });

                // 为表格1绑定事件
                Table.api.bindevent(table2);
                Controller.api.bindevent();
            },



        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
        }
    };
    return Controller;
});